<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>国家级舔狗俱乐部</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hashow('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree default-expand-all :data="list">
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hshow(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hshowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="removeList(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      title="添加"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="40%"
    >
      <addor-edit v-if="dialogVisible" :origin-list="originList" :mwj="curId" :isedit="isEdit" @success="hsuccess" />
    </el-dialog>
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisibleEdit"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="40%"
    >
      <addor-edit v-if="dialogVisibleEdit" :origin-list="originList" :mwj="curId" :isedit="isEdit" @success="hsuccess" />
    </el-dialog>
  </div>
</template>
<script>
import { deleteDepartments, getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddorEdit from './deptstion.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      // 依赖一份树形数据
      // list: [{
      //   name: '财务部',
      //   manager: '刘备',
      //   children: [
      //     {
      //       name: '财务核算部',
      //       manager: '张飞'
      //     },
      //     {
      //       name: '税务核算部',
      //       manager: '关羽'
      //     }
      //   ]
      // }]
      list: [],
      dialogVisible: false,
      dialogVisibleEdit: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const { data: res } = await getDepartments()
      // console.log(res.data.depts.shift())
      res.data.depts.shift()
      this.originList = res.data.depts.map(({ id, pid, code, name }) => { return { id, pid, name, code } })
      this.list = tranListToTreeData(res.data.depts)
    },
    hashow(id) {
      this.dialogVisible = true
      this.curId = id
      this.isEdit = false
    },
    hshow(id) {
      this.dialogVisible = true
      this.curId = id
      this.isEdit = false
    },
    hsuccess() {
      this.dialogVisible = false
      this.dialogVisibleEdit = false
      this.loadDepartments()
    },
    hshowEdit(id) {
      this.dialogVisibleEdit = true
      this.isEdit = true
      this.curId = id
    },
    removeList(id) {
      this.$confirm('真的要删除吗？吊毛', '提示', {
        type: 'warning'
      }).then(async() => {
        const { data: res } = await deleteDepartments(id)
        this.$message.success(res.message)
        this.loadDepartments()
      }
      ).catch((e) => {
        this.$message.error(e.message)
      })
    }
  }
}
</script>
